package com.kj.tms.system.controller.org;


import com.kj.tms.common.bean.dto.Pager;
import com.kj.tms.common.bean.dto.ReturnData;
import com.kj.tms.common.bean.dto.TreeData;
import com.kj.tms.common.utils.StringUtil;
import com.kj.tms.common.utils.constant.Consts;
import com.kj.tms.pri.system.basicinfo.dto.TaxPayersInfoDto;
import com.kj.tms.pri.system.basicinfo.service.TaxPayersInfoService;
import com.kj.tms.pri.system.org.dto.OrgDto;
import com.kj.tms.pri.system.org.service.OrgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dongshidong
 * @ProjectName: tms
 * @Package: com.hongtu.tms.system.controller.org
 * @ClassName: OrgController
 * @Description: 组织机构OrgController
 * @CreateDate: 2017/11/08 9:38
 * @UpdateUser:
 * @UpdateDate:
 * @UpdateRemark:
 * @Version: 1.0
 * Copyright: 北京宏图天安 Copyright (c) 2017
 */
@Controller
@RequestMapping("/system/org")
public class OrgController {
    @Autowired
    private OrgService orgService;
    @Autowired
    private TaxPayersInfoService taxPayersInfoService;

    /**
     * @param
     * @return String
     * @throws
     * @method index
     * @description 首页
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/index")
    public String index() {
        return "system/org/index";
    }

    /**
     * @param
     * @return String
     * @throws
     * @method toAdd
     * @description 新增
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        return "system/org/add";
    }

    /**
     * @param
     * @return String
     * @throws
     * @method toUpdate
     * @description 更新
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/toUpdate")
    public String toUpdate() {
        return "system/org/update";
    }

    /**
     * @param model
     * @return ReturnData
     * @throws
     * @method queryByList
     * @description 查询数据到列表
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/queryByList")
    @ResponseBody
    public ReturnData queryByList(OrgDto model) {
        List<OrgDto> dataList = null;
        try {
            dataList = orgService.listByOrg(model);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "查询机构数据到列表失败", "");
        }
        //设置页面数据
        Map<String, Object> returnObject = new HashMap<String, Object>();
        returnObject.put("rows", dataList);
        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }

    /**
     * @param model
     * @return ReturnData
     * @throws
     * @method listByPage
     * @description 查询分页数据
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/listByPage")
    @ResponseBody
    public ReturnData listByPage(OrgDto model,@RequestParam(value = "isDisplayAll",required = false) Integer isDisplayAll) {
        if (StringUtil.isNotNull(isDisplayAll) && isDisplayAll == 1) {
            // 显示所有下级节点
            model.setPcode(null);
        }
        Pager pager = null;
        try {
            pager = orgService.listByPage(model);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "查询机构分页数据失败", "");
        }
        //设置页面数据
        Map<String, Object> returnObject = new HashMap<String, Object>();
        returnObject.put("total", pager.getRowCount());
        returnObject.put("rows", pager.getResults());
        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }

    /**
     * @param dto
     * @return ReturnData
     * @throws
     * @method save
     * @description 添加或修改数据
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/save")
    @ResponseBody
    public ReturnData save(OrgDto dto) {
        Map<String, Object> orgMap = new HashMap<String, Object>();
        ReturnData rtn = new ReturnData();
        if (StringUtils.isBlank(dto.getId())) {
            return rtn = orgService.insertDto(dto);
        } else {
            return rtn = orgService.updateBySelective(dto);
        }
    }

    /**
     * @param id，validMark
     * @return ReturnData
     * @throws
     * @method updateByValidMark
     * @description 启用禁用
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/updateByValidMark")
    @ResponseBody
    public ReturnData updateByValidMark(@RequestParam(value = "id",required = false) String id,
                                        @RequestParam(value = "validMark",required = false) String validMark) {
        OrgDto dto = new OrgDto();
        dto.setId(id);
        if (validMark.equals(Consts.VALID_MARK_1)) {
            dto.setValidMark(Consts.VALID_MARK_0);
        } else {
            dto.setValidMark(Consts.VALID_MARK_1);
        }
        try {
            orgService.updateByValidMark(dto);
        } catch (Exception e) {
            return new ReturnData(Consts.SUCCESS, "启用禁用失败", null);
        }
        return new ReturnData(Consts.SUCCESS, "启用禁用成功", null);
    }

    /**
     * @param id
     * @return ReturnData
     * @throws
     * @method toDeleteOrg
     * @description 逻辑删除
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/toDeleteOrg")
    @ResponseBody
    public ReturnData deleteOrg(@RequestParam(value = "id",required = false) String id,
                                @RequestParam(value = "orgCode",required = false) String orgCode) {
        OrgDto dto = new OrgDto();
        dto.setOrgCode(orgCode);
        dto.setId(id);
        ReturnData rtn = new ReturnData();
        try {
             rtn = orgService.deleteOrg(dto);

            //清空税号和组织关系
            List<TaxPayersInfoDto> list = taxPayersInfoService.queryListByOrgCode(orgCode);
            if (list.size() != 0) {
                rtn = taxPayersInfoService.updateToDelOrgRelease(orgCode);
                return rtn;
            } else {
                return rtn;
            }

        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "删除失败！", null);
        }
    }

    /**
     * @param model
     * @return ReturnData
     * @throws
     * @method getTreeData
     * @description 获取组织机构树信息
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/getTreeData")
    @ResponseBody
    public ReturnData getTreeData(OrgDto model) {
        List<TreeData> list = null;
        try {
            list = orgService.getTreeData(model);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "获取组织机构树信息失败", "");
        }
        return new ReturnData(Consts.SUCCESS, "", list);
    }

    /**
     * @param model
     * @return ReturnData
     * @throws
     * @method getDirectChildrenTreeData
     * @description 获取组织机构树信息
     * @author 黄冬冬
     * @version 1.0
     * @date
     */
    @RequestMapping("/getDirectChildrenTreeData")
    @ResponseBody
    public ReturnData getDirectChildrenTreeData(OrgDto model) {
        List<TreeData> list = null;
        try {
            list = orgService.getDirectChildrenTreeData(model);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "获取组织机构树信息失败", "");
        }
        return new ReturnData(Consts.SUCCESS, "", list);
    }

    /**
     * @param userCode
     * @return ReturnData
     * @throws
     * @method getTreeDataByUserCode
     * @description 通过查询用户编码获取组织机构树信息
     * @author 董世栋
     * @version 1.0
     * @date
     */
    @RequestMapping("/getTreeDataByUserCode")
    @ResponseBody
    public ReturnData getTreeDataByUserCode(@RequestParam(value = "userCode", required = false) String userCode) {
        List<TreeData> list = null;
        try {
            list = orgService.getTreeDataByUserCode(userCode);
            if (list.size() == 0) {
                return new ReturnData(Consts.ERROR, "通过该用户编码没有维护组织机构", "");

            }
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "获取组织机构树信息异常", "");
        }
        return new ReturnData(Consts.SUCCESS, "", list);
    }
}
